package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.rainfall.domain.RainfallWarning;
import com.sjgtw.cloud.web.rainfall.service.IRainfallInspectionService;
import com.sjgtw.cloud.web.rainfall.service.IRainfallWarningService;
import com.sjgtw.cloud.web.rainfall.vo.RainfallVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 降雨预警Controller
 * 
 * @author lvyoufang
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/app/rainfall")
@Api(tags = "6.2.1、APP降雨预警")
public class AppRainfallWarningController extends BaseController
{
    @Autowired
    private IRainfallWarningService rainfallWarningService;

    @Autowired
    private IRainfallInspectionService rainfallInspectionService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询降雨预警列表
     */
    @ApiOperation(value = "查询降雨预警列表(submited 为true 时已处置 空或者FALSE为 未处置)")
    @GetMapping("/list")
    public TableDataInfo list(RainfallWarning rainfallWarning)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        Long userId = user.getUserid();

        if (userId != null) {
            rainfallWarning.setResponsibleUserId(userId + "");
        }
        startPage();
        List<RainfallVO> list = rainfallWarningService.selectRainfallList(rainfallWarning);
        return getDataTable(list);
    }

    /**
     * 获取降雨预警详细信息
     */
    @ApiOperation(value = "获取降雨预警详细信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(rainfallWarningService.selectRainfallById(id));
    }

    /**
     * 处置
     */
    @ApiOperation(value = "处置")
    @PostMapping("/deal")
    public AjaxResult deal(@RequestBody RainfallVO vo)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        Long userId = user.getUserid();

        vo.setProcessUserIds(userId+"");
        vo.setResponsibleUserId(userId+"");
        return toAjax(rainfallWarningService.deal(vo));
    }


}
